//
//  HYCPudController.m
//  FamilyTravel
//
//  Created by dllo on 15/10/30.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HYCPudController.h"
#import "HYCHeadDocument.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface HYCPudController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) NSMutableArray *arr;
@property(nonatomic, assign) NSInteger page;
@property(nonatomic, retain) MBProgressHUD *hud;

@end

@implementation HYCPudController
- (void)dealloc {
    [_str release];
    [_tableView release];
    [_arr release];
    [_hud release];
    
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    

    self.arr = [NSMutableArray array];
    self.page = 1;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self createData];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"加载中...";
    
    
    // 上拉刷新下拉加载
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(HeaderAction)];
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.page++;
        [self createData];
    }];
    
}


// 刷新
-(void)HeaderAction{
    [self.arr removeAllObjects];
    [self createData];
}

- (void)createData {
    

    NSString *str = [NSString stringWithFormat:@"%@%@",@"http://open.qyer.com/qyer/hotel/search_list?checkin=2015-11-27%2000%3A00%3A01&checkout=2015-11-28%2023%3A59%3A59&city_id=", self.str];
    
    NSString *urlTwo = [NSString stringWithFormat:@"&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=%d", self.page * 20];
    
    NSString *urlOne = [NSString stringWithFormat:@"&from_key=app_ios_index_top&orderby=1&page=%ld", (long)self.page];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@%@%@",str, urlTwo,urlOne, @"&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=AB5B28ED-8670-4190-873A-F09EFE3EB3E8&track_os=ios%25207.1.2&v=1"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:urlStr parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.arr = [HYCPudPushModel baseModelByArr:dic[@"data"][@"hotel"]];
        
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [self.hud removeFromSuperview];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.arr.count != 0) {
        HYCPushDisDetailsController *pDDVC = [[HYCPushDisDetailsController alloc] init];
        
        NSString *str = [NSString stringWithFormat:@"%@%@", @"http://www.qyer.com/goto.php?url=http%3A%2F%2Fwww.booking.com%2Fhotel%2Ffr%2Fst-christopher-s-inn-paris-gare-du-nord.html%3Faid%3D361066%26si%3Dai%252Cco%252Cci%252Cre%252Cdi%26label%3Dguid_feb61f78-4c71-a81c-2ea2-1d54424b9f1b_datetime_14462015984117_cityid_20_hotelid_446553_device_AB5B28ED-8670-4190-873A-F09EFE3EB3E8%26lang%3Dzh-cn%26ifl%3D%26ss%3D%26checkin_monthday%3D27%26checkin_year_month%3D2015-11%26checkout_monthday%3D28%26checkout_year_month%3D2015-11&hotel_name=%E5%9C%A3%E5%85%8B%E9%87%8C%E6%96%AF%E6%89%98%E5%BC%97%E5%B7%B4%E9%BB%8E%E9%85%92%E5%BA%97%E5%B7%B4%E9%BB%8E%E5%8C%97%E7%AB%99+&hotelid=", [self.arr[indexPath.row] id]];
        
        pDDVC.wStr = str;
        
        
        [self.navigationController pushViewController:pDDVC animated:YES];
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    HYCPudPushCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[HYCPudPushCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    
    if (self.arr.count != 0) {
        HYCPudPushModel *model = self.arr[indexPath.row];
        cell.model = model;
        
        cell.layer.shadowColor = [[UIColor blackColor]CGColor];
        cell.alpha = 0;
        [UIView beginAnimations:@"rotation" context:NULL];
        [UIView setAnimationDuration:1];
        cell.alpha = 1;
        [UIView commitAnimations];
    }
 
    return  cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return HEIGHT / 5;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
